// 导入各项模块
const fs = require('fs')
const http = require('http')
const path = require('path')

// 新建http服务
// let 
let server = http.createServer(function (request,response) {
  let requestUrl = decodeURI(request.url)
  console.log(requestUrl);
  console.log(__dirname);
  let fullPath = path.join(__dirname,'./web',requestUrl)
  console.log(fullPath);

  
  fs.readFile(fullPath,function (err, data) {
    if (err == null) {
      // 找得到就给你
      response.end(data);
    }else{
      response.setHeader('content-type','text/html;charset=utf-8');
      // 找不到就 提示404
      response.end(`
        <h1> 404 你要的页面去火星了</h1>
      `)
    }
  })
})

server.listen('8848', function () {
  console.log('开启成功');
})